home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MAZE__ / CMAZEAPP.C < prev    next >
Text File  |  1991-12-29  |  9KB  |  328 lines

  1. /*****
  2.  * CMazeApp.c
  3.  *
  4.  *    Application methods for a typical application.
  5.  *
  6.  *  Copyright ⌐ 1990 Symantec Corporation.  All rights reserved.
  7.  *
  8.  *****/
  9.  
  10. #include "CMazeApp.h"
  11. #include "CMazeDoc.h"
  12. #include "CDialogDirector.h"
  13. #include "CDLOGDirector.h"
  14. #include "CDLOGDialog.h"
  15. #include "CDialog.h"
  16. #include "CDialogText.h"
  17. #include "CBartender.h"
  18. #include "Commands.h"
  19. #include "CApplication.h"
  20. #include "CDesktop.h"
  21. #include "TCLUtilities.h"
  22. #include "CDecorator.h"
  23.  
  24. extern    OSType    gSignature;
  25.  
  26. #define        kExtraMasters        8
  27. #define        kRainyDayFund        20480
  28. #define        kCriticalBalance    20480
  29. #define        kToolboxBalance        20480
  30.  
  31. #define        kAboutDLOG            130
  32.  
  33. extern        CDesktop        *gDesktop;  /* The enclosure for all windows */
  34.  
  35. CDLOGDirector    *aboutDialogDirector;
  36. CDLOGDialog        *aboutDialog;
  37.  
  38. /***
  39.  * IMazeApp
  40.  *
  41.  *    Initialize the application. Your initialization method should
  42.  *    at least call the inherited method. If your application class
  43.  *    defines its own instance variables or global variables, this
  44.  *    is a good place to initialize them.
  45.  *
  46.  ***/
  47.  
  48. void CMazeApp::IMazeApp(void)
  49.  
  50. {
  51.     CApplication::IApplication( kExtraMasters, kRainyDayFund, 
  52.                         kCriticalBalance, kToolboxBalance);    
  53.  
  54. /*  The parameters to IApplication are the number of times to call  
  55.     MoreMasters, the total number of bytes of heap space to reserve for                       
  56.     monitoring low memory situations, and the portion of the memory
  57.     reserve to set aside for critical operations and toolbox calls.
  58.     
  59.     Four (4) is a reasonable number of MoreMasters calls,                           
  60.     but you should determine a good number for your application                     
  61.     by observing the heap using Lightsbug,                                              
  62.     TMON, or Macsbug. Set this parameter to zero, give your                         
  63.     program a rigorous work-out, then look at the heap and count                        
  64.     how many master pointer blocks have been allocated. Master                      
  65.     pointer blocks are nonrelocatable and have a size of $100                           
  66.     (hex). You should call MoreMasters at least this many                               
  67.     times -- add a few extra just to be safe. The purpose of all                        
  68.     this preflighting is to prevent heap fragmentation. You                             
  69.     don't want the Memory Manager to call MoreMasters and                           
  70.     create a nonrelocatable block in the middle of your heap. By                        
  71.     calling MoreMasters at the very beginning of the program,                           
  72.     you ensure that these blocks are allocated in a group at the                        
  73.     bottom of the heap. 
  74.                                                                         
  75.     The memory reserve is a safeguard for handling low memory                   
  76.     conditions and is used by the GrowMemory method in                              
  77.     CApplication (check there for more comments). In general,                           
  78.     your program should never request a memory block greater                        
  79.     than this reserve size without explicitly checking in                               
  80.     advance whether there is enough free memory to satisfy the                      
  81.     the request.
  82.                                                                                     
  83.  */
  84.  
  85. }
  86.  
  87.  
  88.  
  89. /***
  90.  * SetUpFileParameters
  91.  *
  92.  *    In this routine, you specify the kinds of files your
  93.  *    application opens.
  94.  *
  95.  *
  96.  ***/
  97.  
  98. void CMazeApp::SetUpFileParameters(void)
  99.  
  100. {
  101.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  102.  
  103.         /**
  104.          **    sfNumTypes is the number of file types
  105.          **    your application knows about.
  106.          **    sfFileTypes[] is an array of file types.
  107.          **    You can define up to 4 file types in
  108.          **    sfFileTypes[].
  109.          **
  110.          **/
  111.  
  112.     sfNumTypes = 1;
  113.     sfFileTypes[0] = 'MAZE';
  114.  
  115.         /**
  116.          **    Although it's not an instance variable,
  117.          **    this method is a good place to set the
  118.          **    gSignature global variable. Set this global
  119.          **    to your application's signature. You'll use it
  120.          **    to create a file (see CFile::CreateNew()).
  121.          **
  122.          **/
  123.  
  124.     gSignature = 'MAZE';
  125. }
  126.  
  127.  
  128. /***
  129.  * SetUpMenus 
  130.  *
  131.  * Set up menus which must be created at run time, such as a
  132.  * Font menu. You can eliminate this method if your application
  133.  * does not have any such menus.
  134.  *
  135. ***/
  136.  
  137.  void CMazeApp::SetUpMenus()
  138.  {
  139.  
  140.   inherited::SetUpMenus();  /*  Superclass takes care of adding     
  141.                                 menus specified in a MBAR id = 1    
  142.                                 resource    
  143.                             */                          
  144.  
  145.         /* Add your code for creating run-time menus here */    
  146.  }
  147.  
  148.  
  149.  
  150. /***
  151.  * DoCommand
  152.  *
  153.  *    Your application will probably handle its own commands.
  154.  *    Remember, the command numbers from 1-1023 are reserved.
  155.  *  The file Commands.h contains all the predefined TCL
  156.  *  commands.
  157.  *
  158.  *    Be sure to call the default method, so you can get
  159.  *    the default behvior for standard commands.
  160.  *
  161.  ***/
  162. void CMazeApp::DoCommand(long theCommand)
  163.  
  164. {
  165.     CDLOGDirector    *aboutDialogDirector;
  166.     CDLOGDialog        *aboutDialog;
  167.  
  168.     switch (theCommand) {
  169.  
  170.         case cmdAbout:
  171.             aboutDialogDirector = new( CDLOGDirector);
  172.             aboutDialogDirector->IDLOGDirector(kAboutDLOG, this);
  173.             aboutDialog = new(CDLOGDialog);
  174.             aboutDialog->IDLOGDialog(kAboutDLOG, gDesktop,
  175.                                         aboutDialogDirector);
  176.             aboutDialog->Show();
  177.             aboutDialog->Select();
  178.             
  179.         break;
  180.     
  181.         default:    inherited::DoCommand(theCommand);
  182.                     break;
  183.     }
  184. }
  185.  
  186.  
  187. /***
  188.  *
  189.  * UpdateMenus 
  190.  *
  191.  *   Perform menu management tasks
  192.  *
  193. ***/
  194.  
  195.  void CMazeApp::UpdateMenus()
  196.  {
  197.   inherited::UpdateMenus();     /* Enable standard commands */ 
  198.  }
  199.  
  200.  
  201. /***
  202.  * Exit
  203.  *
  204.  *    Chances are you won't need this method.
  205.  *    This is the last chance your application gets to clean up
  206.  *  things like temporary files before terminating.
  207.  *
  208.  ***/
  209.  
  210. void CMazeApp::Exit()
  211.  
  212. {
  213.     /* your exit handler here */
  214. }
  215.  
  216.  
  217. /***
  218.  * CreateDocument
  219.  *
  220.  *    The user chose New from the File menu.
  221.  *    In this method, you need to create a document and send it
  222.  *    a NewFile() message.
  223.  *
  224.  ***/
  225.  
  226. void CMazeApp::CreateDocument()
  227.  
  228. {
  229.     CMazeDoc    *theDocument = NULL;
  230.     
  231.     TRY
  232.     {
  233.         theDocument = new(CMazeDoc);
  234.             
  235.             /**
  236.              **    Send your document an initialization
  237.              **    message. The first argument is the
  238.              **    supervisor (the application). The second
  239.              **    argument is TRUE if the document is printable.
  240.              **
  241.              **/
  242.         
  243.         theDocument->IMazeDoc(this, TRUE);    
  244.             /**
  245.              **    Send the document a NewFile() message.
  246.              **    The document will open a window, and
  247.              **    set up the heart of the application.
  248.              **
  249.              **/
  250.         theDocument->NewFile();
  251.     }
  252.     
  253.     CATCH
  254.     {
  255.         /*
  256.          * This exception handler gets executed if a failure occurred 
  257.          * anywhere within the scope of the TRY block above. Since 
  258.          * this indicates that a new doc could not be created, we
  259.          * check if an object had been allocated and if it has, send 
  260.          * it a Dispose message. The exception will propagate up to
  261.          * CSwitchboard's exception handler, which handles displaying
  262.          * an error alert.
  263.          */
  264.          
  265.          if (theDocument) theDocument->Dispose();
  266.  
  267.     }
  268.     ENDTRY;
  269. }
  270.  
  271. /***
  272.  * OpenDocument
  273.  *
  274.  *    The user chose Open╔ from the File menu.
  275.  *    In this method you need to create a document
  276.  *    and send it an OpenFile() message.
  277.  *
  278.  *    The macSFReply is a good SFReply record that contains
  279.  *    the name and vRefNum of the file the user chose to
  280.  *    open.
  281.  *
  282.  ***/
  283.  
  284. void CMazeApp::OpenDocument(SFReply *macSFReply)
  285.  
  286. {
  287.     CMazeDoc    *theDocument = NULL;
  288.     
  289.     TRY
  290.     {
  291.     
  292.         theDocument = new(CMazeDoc);
  293.             
  294.             /**
  295.              **    Send your document an initialization
  296.              **    message. The first argument is the
  297.              **    supervisor (the application). The second
  298.              **    argument is TRUE if the document is printable.
  299.              **
  300.              **/
  301.         
  302.         theDocument->IMazeDoc(this, TRUE);
  303.     
  304.             /**
  305.              **    Send the document an OpenFile() message.
  306.              **    The document will open a window, open
  307.              **    the file specified in the macSFReply record,
  308.              **    and display it in its window.
  309.              **
  310.              **/
  311.         theDocument->OpenFile(macSFReply);
  312.     }
  313.     
  314.     CATCH
  315.     {
  316.         /*
  317.          * This exception handler gets executed if a failure occurred 
  318.          * anywhere within the scope of the TRY block above. Since 
  319.          * this indicates that the document could not be opened, we
  320.          * send it a Dispose message. The exception will propagate up to
  321.          * CSwitchboard's exception handler, which handles displaying
  322.          * an error alert.
  323.          */
  324.          
  325.          if (theDocument) theDocument->Dispose();
  326.     }
  327.     ENDTRY;
  328. }